package cn.skylark.wcm.common.cos;

import cn.skylark.wcm.entity.Tenanproperty;
import cn.skylark.wcm.entity.User;
import cn.skylark.wcm.service.TenanPropertyService;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.sign.Credentials;
import org.apache.commons.beanutils.BeanMap;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * CosConfig
 *
 * @author chuhl
 * @date 2017/12/19
 */
public class CosConfig {
  /** logger */
  private static final Logger LOGGER = LoggerFactory.getLogger(CosConfig.class);

  /**
   * bucketName
   */
  public String bucketName;
  /**
   * tenanid
   */
  public Integer tenanid;
  /**
   * userid
   */
  public Integer userid;
  /**
   * appid
   */
  private long appid;
  /**
   * secretid
   */
  private String secretid;
  /**
   * secretkey
   */
  private String secretkey;
  /**
   * region
   */
  private String region;

  /**
   *
   * @return COSClient
   */
  public COSClient getCosClient(TenanPropertyService tenanPropertyService,User user) {
    LOGGER.info("腾讯云COS属性获取 开始-------- tenanid = " + user.getTenanid() + ", userid=" + user.getId());
    tenanid = user.getTenanid();
    userid =  user.getId();
    List<Tenanproperty> tenanpropertyList = tenanPropertyService.getTenanPropertyByTenanId(tenanid);
    for (Tenanproperty tenanproperty : tenanpropertyList) {
      switch (tenanproperty.getKeycode()) {
        case "AppId":
          appid = Long.valueOf(tenanproperty.getKeyvalue());
          break;
        case "SecretId":
          secretid = tenanproperty.getKeyvalue();
          break;
        case "SecretKey":
          secretkey = tenanproperty.getKeyvalue();
          break;
        case "Region":
          region = tenanproperty.getKeyvalue();
          break;
        case "BucketName":
          bucketName = tenanproperty.getKeyvalue();
          break;
        default:
          break;
      }
    }
    Credentials cred = new Credentials(appid, secretid, secretkey);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setRegion(region);

    COSClient cosClient = new COSClient(clientConfig, cred);
    LOGGER.info("腾讯云COS属性获取 结束-------- ");
    return cosClient;
  }
}
